// pages/order/order.js
Page({

  data: {
    nowstate:'',
  // 订单状态
    sort_part:[
      {
        index:0,
        text:"全部",
        border_change:''
      },{
        index:1,
        text:"待付款",
        border_change:''
      },{
        index:2,
        text:"待发货",
        border_change:''
      },{
        index:3,
        text:"租赁中",
        border_change:''
      },{
        index:4,
        text:"待归还",
        border_change:''
      },{
        index:5,
        text:"待评价",
        border_change:''
      },{
        index:6,
        text:"售后服务",
        border_change:''
      }
    ],
    // 全部订单
    allorder:[],
    // 待支付
    nopay:[],
    //待发货
    waititem:[]
  },
  onLoad(options) {
    var that = this;
    var index = parseInt(options.index) + 1;
    // console.log(index);
    var initvalue = 'sort_part['+index+'].border_change';
    this.setData({
     [initvalue]:' color:orange; font-weight:bold; font-size:large;',
     nowstate:this.data.sort_part[index].text
    })


    //进入渲染待付款
    if(index == 0){
      wx.request({
        url: 'http://127.0.0.1:8080/order/selectAllOrderById',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            // console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              allorder:res.data
            })
          }
      })
    }else if(index == 1){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderNoPay',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            // console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              nopay:res.data
            })
          }
      })
    }else if(index == 2){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderWaitItem',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){

            for(var i=0;i<res.data.length;i++){
              res.data[i].ismine = true;
              if(res.data[i].outUserId != wx.getStorageSync('openid')){
                res.data[i].ismine = false;
              }
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            // console.log(res)
            that.setData({
              waititem:res.data
            })
          }
      })
    }else if(index == 3){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderInRent',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            // console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].ismine = true;
              if(res.data[i].outUserId != wx.getStorageSync('openid')){
                res.data[i].ismine = false;}
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              waititem:res.data
            })
          }
      })
    }else if(index == 4){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderBack',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].ismine = true;
              if(res.data[i].outUserId != wx.getStorageSync('openid')){
                res.data[i].ismine = false;}
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              waititem:res.data
            })
          }
      })
    }else if(index == 5){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderFinish',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
              // if(res.data[i].order)
            }
            
            that.setData({
              waititem:res.data
            })
          }
      })
    }else if(index == 6){
      // 被租赁物品的订单
      wx.request({
        url: 'http://127.0.0.1:8080/order/mineRent',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(result){
            // console.log(result)
            for(var i=0;i<result.data.length;i++){
              result.data[i].ismine = true;
              if(result.data[i].outUserId != wx.getStorageSync('openid')){
                result.data[i].ismine = false;}
              var orderinfo = result.data[i];
              //图片格式更改
              orderinfo.itemPic = JSON.parse(orderinfo.itemPic)
            }
            that.setData({
              waititem:result.data
            })
            
          }
      })
    }
  },

    //模块选择框
  change_color:function (e) {
    var that  = this;
      for(var i=0;i<8;i++){
        var value = 'sort_part['+i+'].border_change';
        this.setData({
          [value]:''
        })
      }
      var index = e.currentTarget.dataset.index;
      var value = 'sort_part['+index+'].border_change';
      this.setData({
        [value]:'color:orange; font-weight:bold; font-size:large;',
        nowstate:this.data.sort_part[index].text
      })

          //进入渲染待付款
    if(index == 0){
      wx.request({
        url: 'http://127.0.0.1:8080/order/selectAllOrderById',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            // console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              allorder:res.data
            })
          }
      })
    }else if(index == 1){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderNoPay',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              nopay:res.data
            })
          }
      })
    }else if(index == 2){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderWaitItem',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            // console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].ismine = true;
              if(res.data[i].outUserId != wx.getStorageSync('openid')){
                res.data[i].ismine = false;
              }
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              waititem:res.data
            })
          }
      })
    }else if(index == 3){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderInRent',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            // console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].ismine = true;
              if(res.data[i].outUserId != wx.getStorageSync('openid')){
                res.data[i].ismine = false;}
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              waititem:res.data
            })
          }
      })
    }else if(index == 4){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderBack',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            // console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].ismine = true;
              if(res.data[i].outUserId != wx.getStorageSync('openid')){
                res.data[i].ismine = false;}
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              waititem:res.data
            })
          }
      })
    }else if(index == 5){
      wx.request({
        url: 'http://127.0.0.1:8080/order/userOrderFinish',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              waititem:res.data
            })
          }
      })
    }else if(index == 6){
      wx.request({
        url: 'http://127.0.0.1:8080/order/mineRent',
        method:"POST",
        data:{openid:wx.getStorageSync('openid')},
          success(res){
            // console.log(res)
            for(var i=0;i<res.data.length;i++){
              res.data[i].ismine = true;
              if(res.data[i].outUserId != wx.getStorageSync('openid')){
                res.data[i].ismine = false;}
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
            that.setData({
              waititem:res.data
            })
          }
      })
    }
      //按照相应模块进行内容展示
  
      // console.log(e)
  },

  //展示物品信息
  showiteminfo(e){
    var index = e.currentTarget.dataset.index
    // console.log(index)
    console.log(this.data)
    
    var data = this.data
    var itemid = "";
    if(data.nopay.length != 0){
      itemid = data.nopay[index].itemID
    }else if(data.allorder.length != 0){
      itemid = data.allorder[index].itemID
    }else{
      itemid = data.waititem[index].itemID
    }
    console.log(itemid)
    wx.navigateTo({
      url: '/pages/iteminfo/iteminfo?itemid='+itemid,
    })
  },

//待付款部分

  //删除待付款订单
  deloredr(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var orderid = this.data.nopay[index].orderID;
    wx.showModal({
      title: '确定删除该订单?',
      success: (result) => {
        if(result.confirm){
          wx.request({
            url: 'http://127.0.0.1:8080/order/delNoPay',
            method:"POST",
            data:{
              orderid:orderid
            },success(res){
              // console.log(res)
              if(res.data){
                wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })
        }
      },
    })
    
  },
  //付款待支付订单
  pay(e){
    var index = e.currentTarget.dataset.index
    // console.log(this.data.nopay[index])
    var data = this.data.nopay[index];
    wx.navigateTo({
      url: '/pages/rent/rent?orderid='+data.orderID+'&itemid='+data.itemID,
    })
  },

//待发货
  submit(e){
    // console.log(e);
    var index = e.currentTarget.dataset.index;
    var data = this.data.waititem[index];
    // console.log(this.data)
    wx.navigateTo({
      url: '/pages/certify/certify?orderid='+data.orderID+'&itemid='+data.itemID+'&ismine='+data.ismine,
    })
  },

//租赁中
  seeorder(e){
    var index = e.currentTarget.dataset.index;
    var data = this.data.waititem[index];
    wx.navigateTo({
      url: '/pages/rentnow/rentnow?orderid='+data.orderID+'&itemid='+data.itemID+'&ismine='+data.ismine,
    })
  },

//待归还
  return(e){
    var index = e.currentTarget.dataset.index;
    var data = this.data.waititem[index];
    wx.navigateTo({
      url: '/pages/return/return?orderid='+data.orderID+'&itemid='+data.itemID+'&ismine='+data.ismine,
    })
  },

// 待评价
  judge(e){
    var index = e.currentTarget.dataset.index;
    var data = this.data.waititem[index];
    console.log(data)
    console.log(data.userIdForOrder,)
    if(data.userIdForOrder == wx.getStorageSync('openid') || data.orderState == "已评价"){
      wx.navigateTo({
        url: '/pages/judgeitem/judgeitem?orderid='+data.orderID+'&itemid='+data.itemID,
      })
    }else{
      wx.showToast({
        title: '无法评价出租订单',
        icon:"none"
      })
    }
    // console.log(data)
  
  },
  // 展示订单信息
  showorderinfo(e){
    var index = e.currentTarget.dataset.index;
    var data = this.data.waititem[index];
    wx.navigateTo({
      url: '/pages/orderinfo/orderinfo?orderid='+data.orderID+'&itemid='+data.itemID+'&ismine='+data.ismine,
    })
  },
  //售后
  aftersale(e){
    var index = e.currentTarget.dataset.index;
    var data = this.data.waititem[index];
    console.log(data)
    if(data.ismine){
      wx.navigateTo({
        url: '/pages/afterservice/afterservice?orderid='+data.orderID+'&itemid='+data.itemID,
      })
    }else{
      wx.request({
        url: 'http://127.0.0.1:8080/afterService/selectAfterService',
        method:"POST",
        data:{orderid:data.orderID},
        success(res){
          console.log(res.data)
          if(res.data != ""){
            wx.navigateTo({
              url: '/pages/afterservice/afterservice?orderid='+data.orderID+'&itemid='+data.itemID,
            })
          }else{
            wx.showToast({
              title: '该订单未被申请售后',
              icon:"none"
            })
          }
          
        
        }
      })

    }
    
    
  },
  //超过期限无法售后
  noservice(){
    wx.showToast({
      title: '物品归还超过两天无法申请售后',
      icon:"none"
    })
  }
})